package com.ruoyi.system.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.RxBusiness;
import com.ruoyi.system.mapper.RxBusinessMapper;
import com.ruoyi.system.service.IRxBusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 商家Controller
 *
 * @author hjf
 * @date 2025-02-18
 */
@RestController
@RequestMapping("/hmk/business")
public class RxBusinessController extends BaseController {
    @Autowired
    private IRxBusinessService rxBusinessService;

    @Autowired
    private RxBusinessMapper rxBusinessMapper;

    /**
     * 查询商家列表
     */
    @PreAuthorize("@ss.hasPermi('hmk:business:list')")
    @GetMapping("/list")
    public TableDataInfo list(RxBusiness rxBusiness) {
        startPage();
        List<RxBusiness> list = rxBusinessService.selectRxBusinessList(rxBusiness);
        return getDataTable(list);
    }

    @PermitAll
    @GetMapping("/listSimple")
    public AjaxResult listSimple(RxBusiness rxBusiness) {
        List<SysRole> roles = SecurityUtils.getLoginUser().getUser().getRoles();
        List<RxBusiness> list = new ArrayList<>();
        for (SysRole role : roles) {
            if (role.getRoleId().intValue() == 105) {
                rxBusiness.setId(165L);
                list = rxBusinessMapper.selectRxBusinessListSimple(rxBusiness);
                break;
            } else if (role.getRoleId().intValue() == 106) {
                rxBusiness.setId(165L);
                list = rxBusinessMapper.selectRxBusinessListSimple(rxBusiness);
                break;
            }
        }
        if (list.isEmpty()) {
            list = rxBusinessMapper.selectRxBusinessListSimple(rxBusiness);
        }
        return AjaxResult.success(list);
    }


    /**
     * 导出商家列表
     */
    @PreAuthorize("@ss.hasPermi('hmk:business:export')")
    //@Log(title = "商家", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RxBusiness rxBusiness) {
        List<RxBusiness> list = rxBusinessService.selectRxBusinessList(rxBusiness);
        ExcelUtil<RxBusiness> util = new ExcelUtil<RxBusiness>(RxBusiness.class);
        util.exportExcel(response, list, "商家数据");
    }

    /**
     * 获取商家详细信息
     */
    @PreAuthorize("@ss.hasPermi('hmk:business:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(rxBusinessService.selectRxBusinessById(id));
    }

    /**
     * 新增商家
     */
    @PreAuthorize("@ss.hasPermi('hmk:business:add')")
    //@Log(title = "商家", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RxBusiness rxBusiness) {
        return toAjax(rxBusinessService.insertRxBusiness(rxBusiness));
    }

    /**
     * 修改商家
     */
    @PreAuthorize("@ss.hasPermi('hmk:business:edit')")
    //@Log(title = "商家", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RxBusiness rxBusiness) {
        return toAjax(rxBusinessService.updateRxBusiness(rxBusiness));
    }

    /**
     * 删除商家
     */
    @PreAuthorize("@ss.hasPermi('hmk:business:remove')")
    //@Log(title = "商家", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(rxBusinessService.deleteRxBusinessByIds(ids));
    }
}
